﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CRM.DataAccess.Entities;
using CRM.Business.Services;
using CRM.Encryption;

namespace Web.SearchPopUp
{

    public partial class Contact1 : System.Web.UI.Page
    {
        static string requestFind = string.Empty;
        static string Flag = string.Empty;
        static USER user;
        static long? assignedtoID = null;
        static long? OrgID = null;
        static List<long?> ListOrgID ;
        static List<CONTACT_DETAIL> contactDetails;
        static ROLE r;
     
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                return;
            assignedtoID = null;
            user = (USER)Session["User"];
            if (user != null)
            {
                this.RadGrid1.ClientSettings.Selecting.UseClientSelectColumnOnly = true;
                Flag = Request.QueryString["flag"];
                if (Flag != null && Flag.Trim() != "")
                {
                    if (Flag == "true")
                        this.RadGrid1.AllowMultiRowSelection = true;
                }
                DateTime dt = DateTime.Now;
                DateTime dt2 = dt.AddMonths(-3);
                dpDateCreatedFrom.SelectedDate = new DateTime(dt2.Year, dt2.Month, 1);
                dpDateCreatedTo.SelectedDate = DateTime.Now;
                r = RoleService.GetRoleByID((long)user.ROLE_ID);
                if ((!(bool)user.IS_ADMIN) && (!(bool)r.IS_MANAGER))
                    assignedtoID = user.ID;

                requestFind = Request.QueryString["OrgID"];
                if (requestFind != null && requestFind.Trim() != "")
                {
                    ListOrgID =  new List<long?>();
                    string[]  arr = requestFind.Split(';');
                    foreach (string str in arr)
                    {
                        if (str != "")
                        {
                            ListOrgID.Add(Convert.ToInt64(str));
                        }
                    }
                    
                }
                else ListOrgID = new List<long?>();
                contactDetails = getDatasource(dpDateCreatedFrom.SelectedDate, dpDateCreatedTo.SelectedDate.Value.AddHours(24), "", "",ListOrgID,"", "",  assignedtoID);
                RadGrid1.DataSource = contactDetails;
                RadGrid1.DataBind();
            }
        }
      
        protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            RadGrid1.DataSource = contactDetails;
        }
        private List<CONTACT_DETAIL> getDatasource(DateTime? dateFrom, DateTime? dateTo,  string surname, string name, List<long?> ListOrgID,  
            string primaryemail, string officephone, long? assignid)
        {
            List<CONTACT_DETAIL> dt = ContactService.GetContactByConditionForPopUpWindow(dateFrom, dateTo,  surname, name, ListOrgID, primaryemail, officephone, assignid);
            if (dt == null)
                dt = new List<CONTACT_DETAIL>();
            return dt;
        }

        protected void btnSearch_ServerClick(object sender, EventArgs e)
        {
            contactDetails = getDatasource(dpDateCreatedFrom.SelectedDate, dpDateCreatedTo.SelectedDate.Value.AddHours(24), txtSurName.Text, txtName.Text, ListOrgID, txtPrimaryEmail.Text, "", assignedtoID);
            RadGrid1.DataSource = contactDetails;
            RadGrid1.DataBind();
        }

        protected void RadAjaxManager1_AjaxRequest(object sender, Telerik.Web.UI.AjaxRequestEventArgs e)
        {
            if (e.Argument == "InitialPageLoad")
            {
                //simulate longer page load        
                System.Threading.Thread.Sleep(1000);
                Panel1.Style["display"] = "block";
            }
        }
    }
}